package assignment.ton.gui;

import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.ButtonGroup;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JRadioButton;

import assignment.ton.def.Action;
import assignment.ton.def.Class;
import assignment.ton.def.ClassLink;
/**
 * Called whenever a link is added or edited and displays the choice of one or many
 * @author Tim
 */
public class LinkAddFrame extends JFrame implements ActionListener{
	private Class classA, classB;
	private MainFrame mainFrame;
	private JRadioButton oneButton, manyButton;
	private ButtonGroup group;
	private JButton submit, cancel;
	private static final long serialVersionUID = -3658258424214856320L;
	public LinkAddFrame(MainFrame mF, Class a, Class b){
		this.setVisible(true);
		this.setLayout(new GridBagLayout());
		GridBagConstraints c = new GridBagConstraints();
		c.fill = GridBagConstraints.HORIZONTAL;
		c.insets = new Insets(2,2,2,2);
		this.setResizable(false);
		this.setLocationRelativeTo(mainFrame);
		this.setIconImage(new ImageIcon(getClass().getResource("ton_icon.gif")).getImage());
		this.setTitle("Add Link");
		this.addWindowListener(new WindowAdapter(){ //now make it do something more interesting
			public void windowClosing(WindowEvent e){
					cancel();
			}
		});
		classA = a;
		classB = b;
		mainFrame = mF;
		oneButton = new JRadioButton("One");
		oneButton.setSelected(true);
		oneButton.setActionCommand("One");
		manyButton = new JRadioButton("Many");
		manyButton.setActionCommand("Many");
		group = new ButtonGroup();
		group.add(oneButton);
		group.add(manyButton);
		c.gridx = 0;
		c.gridy = 0;
		this.add(oneButton, c);
		c.gridx = 1;
		this.add(manyButton, c);
		submit = new JButton("Submit");
		c.gridx = 0;
		c.gridy = 1;
		this.add(submit, c);
		submit.addActionListener(this);
		cancel = new JButton("Cancel");
		c.gridx = 1;
		this.add(cancel, c);
		cancel.addActionListener(this);
		pack();
		if(a.hasClass(b)&&mainFrame.getPreviousAction()==Action.ADDLINK){
			cancel();
			this.setVisible(false);
			String error = "Error";
			String eT = "Link already exists";
			JOptionPane.showMessageDialog(this, eT, error, JOptionPane.ERROR_MESSAGE);
		}
	}
	@Override
	public void actionPerformed(ActionEvent e) {
		String actionCommand = e.getActionCommand();
		if(actionCommand.equals("Submit")){
			if(classA.addLink(new ClassLink(classB, manyButton.isSelected()))&&mainFrame.getPreviousAction()==Action.ADDLINK){
				mainFrame.getDrawPanel().repaint();
				cancel();
			}else if(classA.hasClass(classB)&&mainFrame.getPreviousAction()==Action.EDITLINK){
				ClassLink classLink = classA.getLink(classB);
				classLink.setMany(manyButton.isSelected());
				mainFrame.getDrawPanel().repaint();
				cancel();
			}else{
				cancel();
				String error = "Error";
				String eT = "Link already exists";
				JOptionPane.showMessageDialog(this, eT, error, JOptionPane.ERROR_MESSAGE);
			}
		}else if(actionCommand.equals("Cancel")){
			cancel();
		}
	}
	/**
	 * Destroys the frame and resets action
	 */
	public void cancel(){
		mainFrame.setAction(mainFrame.getPrevious2Action());
		this.dispose();
	}
}
